package com.jky.stmall.front.controller;

import com.jky.stmall.front.entity.Addr;
import com.jky.stmall.front.entity.Cart;
import com.jky.stmall.front.entity.Order;
import com.jky.stmall.front.entity.User;
import com.jky.stmall.front.service.AddrService;
import com.jky.stmall.front.service.CartService;
import com.jky.stmall.front.service.OrderService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/order")
@AllArgsConstructor
public class OrderController {
    private AddrService addrService;
    private CartService cartService;
    private OrderService orderService;
    @PostMapping("/confirm")
    public String confirm(HttpSession session, Model model,Integer... cartId) {
        //当前用户的地址信息 在地址表中
        User user = (User) session.getAttribute("user");
        List<Addr> addresses=addrService.getByUserId(user.getId());
        model.addAttribute("addresses",addresses);
        //渲染时 model将数据 交给request

        //需要购物车的信息
        try {
            List<Cart> carts=cartService.getByIds(cartId,user.getId());
            //数据获取成功
            model.addAttribute("carts",carts);
            return "user/orderConfirm";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error" ,e.getMessage());
            return "user/error";
        }

    }
    //生成订单的页面
    @GetMapping("/add")
    public String add(HttpSession session,Model model,Integer addressId,Integer... cartId){

        //当前登录用户
        User user= (User) session.getAttribute("user");

        //调用OrderService生产订单
        Order order=new Order();
        order.setAddrId(addressId);
        order.setUserId(user.getId());

        try {
            orderService.add(order,cartId);
            //Order生成成功
            return "redirect:/user/order/pay?id="+order.getId();
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error",e.getMessage());
            return "user/error";
        }

    }
    @GetMapping("/pay")
    public String pay(String id,Model model){
        Order order=orderService.getById(id);
        model.addAttribute("order",order);
        return "user/pay";
    }

    //支付的操作
    @PostMapping("/pay")
    public String pay(String id,String paypwd,Model model,HttpSession session) {
        User user =(User) session.getAttribute("user");
        //实现支付的操作
        try {
            orderService.pay(id,user.getId(),paypwd);
            //支付成功跳转到订单列表
            return "redirect:/user/order/list";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error",e.getMessage());
            return "user/error";

        }
    }
    @GetMapping("/list")
    public String list(){
        return "user/orderList";
    }
}
